<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">标题</label>
        <el-input v-model="query.title" clearable placeholder="标题" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">流程编号</label>
        <el-input v-model="query.flowNo" clearable placeholder="流程编号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <date-range-picker
          v-model="query.publishTime"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          class="date-item"
        />

        <label class="el-form-item-label">状态</label>
        <el-select v-model="query.flowStatus" multiple class="filter-item" filterable clearable placeholder="请选择" @change="crud.toQuery">
          <el-option
            v-for="item in dict.flow_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation>
        <el-button
          slot="left"
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-plus"
        >
          <router-link :to="'/publish/apply?publishId=-1'">
            新增
          </router-link>
        </el-button>
      </crudOperation>

      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column prop="id" label="id" />
        <el-table-column prop="title" label="标题" />
        <el-table-column prop="flowNo" label="流程编号" width="230px" />
        <el-table-column prop="publishTime" label="发布时间" width="150px" />
        <el-table-column v-if="false" prop="applierId" label="申请人id" />
        <el-table-column prop="applierName" label="申请人" />
        <el-table-column v-if="false" prop="projectInfo" label="项目信息" />
        <el-table-column prop="publishType" label="发布类型">
          <template slot-scope="scope">
            {{ dict.label.publish_type[scope.row.publishType] }}
          </template>
        </el-table-column>
        <el-table-column prop="flowNodeId" label="流程节点" width="100px">
          <template slot-scope="scope">
            {{ dict.label.flow_node[`${scope.row.flowNodeId}`] }}
          </template>
        </el-table-column>
        <el-table-column v-if="false" prop="processerId" label="当前处理人id" />
        <el-table-column prop="processerName" label="当前处理人" />
        <el-table-column prop="flowStatus" label="状态">
          <template slot-scope="scope">
            {{ dict.label.flow_status[scope.row.flowStatus] }}
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','publishSheetFlow:edit','publishSheetFlow:del'])" label="操作" width="250px" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="text">
              <router-link :to="'/publish/apply?publishId='+ scope.row.id">
                详情
              </router-link>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudPublishSheetFlow from '@/api/deploy/publishSheetFlow'
import CRUD, { presenter, header, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'
import { del as delPublish } from '@/api/deploy/publishSheetFlow'
import { mapGetters } from 'vuex'
import global_const from '@/mixins/global_const'

export default {
  name: 'FlowList',
  dicts: ['gray_type', 'publish_type', 'approval_handle', 'flow_status', 'flow_node'],
  components: { pagination, crudOperation, rrOperation, DateRangePicker },
  mixins: [presenter(), header(), crud(), global_const],
  cruds() {
    return CRUD({ title: '发布申请单', queryOnPresenterCreated: false, url: 'api/publishSheetFlow', idField: 'id', sort: 'id,desc', crudMethod: { ...crudPublishSheetFlow }, optShow: { add: false, edit: false, del: false, download: false }})
  },
  props: {
    flowQuery: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      permission: {
        add: ['admin', 'publishSheetFlow:add'],
        edit: ['admin', 'publishSheetFlow:edit'],
        del: ['admin', 'publishSheetFlow:del']
      },
      queryTypeOptions: [
        { key: 'title', display_name: '标题，格式如（服务[ztjy-file-serve#ztjy-upload]发布申请-某某人）' },
        { key: 'flowNo', display_name: '流程编号' },
        { key: 'applierId', display_name: '申请人id' }
      ]
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  watch: {
    flowQuery(val) {
      this.query = Object.assign(this.query, val)
    }
  },
  mounted() {
    this.query['flowStatus'] = [`${this.FLOW_STATUS_PROCESS}`, `${this.FLOW_STATUS_REJECT}`]
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    async toDelete(data) {
      await delPublish([data.id])
      this.crud.toQuery()
    }
  }
}
</script>

<style scoped>
</style>
